<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'公告列表'),
			array('op'=>'edit','title'=>'添加公告'),
	);

//公告
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$tip='如果场景前端（包含DIY页面）支持展示公告的时候，前端才会展示出来！';
	$tip .='</br><red>【注意】：本场景已开启独立商品展示，只显示本场景下的公告！</red>';
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$params['plugin_name']=$onepluginname;
			$return['params']['plugin_name']=$onepluginname;
	}else{
			$params['plugin_name OR']='plugin_name=\'jy_weishop\' or plugin_name=\'\' or plugin_name=\''.$_SESSION['current_plugin'].'\'';
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','image','status');
	$list=pdo_getslice('jy_weishop_notice',$params,array($pindex,$psize),$total,$fields);
	if($list){
		foreach($list as &$item){
				$item['url']=custom_url('jy_weishop/shop.noticedetail//id='.$item['id'],true);
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
		//	array('name'=>'paixu','title'=>'顺序','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'url','title'=>'链接地址','type'=>'copy'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
	
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'标题','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['status']=array('0'=>'隐藏','1'=>'显示');
	$return['status2']=array(array('id'=>0,'title'=>'隐藏'),array('id'=>1,'title'=>'显示'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_notice',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回公告列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
				//	array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>''),
					array('type'=>'string','title'=>'标题','name'=>'title','help'=>''),
					array('type'=>'image','title'=>'公告图片','name'=>'image','help'=>''),
					array('type'=>'richtext','title'=>'公告内容','name'=>'content','help'=>''),
					array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),
					
					
				)
			),
		)
	);
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
	//	'paixu'=>intval($postdata['paixu']),
		'status'=>intval($postdata['status']),
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'content'=>trim($postdata['content']),
		'createtime'=>TIMESTAMP,
	);
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin']);
	$data['plugin_name']=$onepluginname?:'jy_weishop';
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_notice',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_notice',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_notice',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的公告！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_weishop_notice',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}